package com.skynet.supervision.aop.uac;

import com.skynet.supervision.aop.uac.beans.UACUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author xuchao
 * @date 2023-03-08 14:45
 */
@SuppressWarnings("unused")
@Slf4j
@Component
public class UACUserContext {
    private static final ThreadLocal<UACUserInfo> THREAD_LOCAL_UACS_USER = new ThreadLocal<>();


    public static void setUser(UACUserInfo user) {
        THREAD_LOCAL_UACS_USER.set(user);
    }

    public static void remove() {
        THREAD_LOCAL_UACS_USER.remove();
    }

    public static UACUserInfo currentUser() {
        return THREAD_LOCAL_UACS_USER.get();
    }

    public static UACUserInfo currentUserOrAnonymous() {
        return currentUserOptional().orElse(UACUserInfo.anonymous());
    }

    public static Optional<UACUserInfo> currentUserOptional() {
        UACUserInfo user = THREAD_LOCAL_UACS_USER.get();
        if (user != null) {
            return Optional.of(user);
        }
        return Optional.empty();
    }

}
